# -*- coding: utf-8 -*-
import logging
import traceback
import sys
from dateutil import parser
import time
from datetime import timedelta
import os

from lxml import etree
import requests
from bs4 import BeautifulSoup

# add up one level dir into sys path
from common.lottery.handler import crawl_from_opencai

sys.path.append(os.path.abspath(os.path.dirname(os.path.dirname(
    os.path.dirname(os.path.dirname(os.path.dirname(__file__)))))))
os.environ['DJANGO_SETTINGS_MODULE'] = 'base.settings'

from common.lottery.cyclical.sd_11x5.db.activity import fullfill_result
from common.lottery import LOTTERY_TYPE, OPENCAI_LOTTERY_TYPES
from common.timer import TIMER_EVENT_TYPE, TimerEvent
from common.utils import tz

URL = 'http://caipiao.163.com/award/11xuan5/'
OFFICIAL_URL = 'http://www.sdticai.com/find/find_syxw.asp'


def _parse_and_fill(resp, refer, date):
    tree = etree.HTML(resp)
    nodes = tree.xpath('//td[@data-award!=""]')
    for node in nodes:
        phase = '%s%s' % (date.strftime('%Y%m%d'), node.text)
        numbers = node.attrib['data-award'].split(' ')
        numbers = [str(int(number)) for number in numbers]  # 移除前导0
        number = ','.join(numbers)
        activity = fullfill_result(phase, number, refer)
        if activity:
            TimerEvent.submit(TIMER_EVENT_TYPE.CALC_STATS, {
                'number': number, 'term': phase,
                'activity_type': LOTTERY_TYPE.SD_11X5}, tz.now_ts())

            TimerEvent.submit(TIMER_EVENT_TYPE.ACTIVITY_ANNOUNCE, {
                'number': number, 'term': phase,
                'activity_type': LOTTERY_TYPE.SD_11X5}, tz.now_ts())


def _parse_and_fill_official(content, refer, now):
    soup = BeautifulSoup(content, 'html5lib')
    trs = soup.select(
        'body > table > tbody > tr > td > table > tbody > tr > td > table > tbody > tr > td > table > tbody > tr > td > table > tbody > tr > td > table > tbody > tr')[
          2:]
    for tr in trs:
        tds = tr.select('td')
        phase = '%s%s' % ('20', tds[0].text)
        numbers = ','.join([str(int(tds[x].text)) for x in range(1, 6)])
        activity = fullfill_result(phase, numbers, refer)
        if activity:
            TimerEvent.submit(TIMER_EVENT_TYPE.CALC_STATS, {
                'number': numbers, 'term': phase,
                'activity_type': LOTTERY_TYPE.SD_11X5}, tz.now_ts())

            TimerEvent.submit(TIMER_EVENT_TYPE.ACTIVITY_ANNOUNCE, {
                'number': numbers, 'term': phase,
                'activity_type': LOTTERY_TYPE.SD_11X5}, tz.now_ts())


def crawl_newest_from_official():
    """
    官网爬虫
    """
    now = tz.local_now()
    refer = OFFICIAL_URL
    response = requests.get(OFFICIAL_URL, timeout=5)
    if response.status_code != 200:
        raise RuntimeError('fail to crawl sd_11x5, need retry...')
    _parse_and_fill_official(response.content, refer, now)
    return True


def crawl_history(date):
    suffix = '%s.html' % date.strftime('%Y%m%d')
    url = URL + suffix
    response = requests.get(url, timeout=10)
    if response.status_code != 200:
        raise RuntimeError("site down")
    _parse_and_fill(response.text, url, date)


def crawl_newest():
    '''163的爬虫
    '''
    now = tz.local_now()
    suffix = '%s.html' % now.strftime('%Y%m%d')
    refer = URL + suffix
    response = requests.get(URL, timeout=5)
    if response.status_code != 200:
        raise RuntimeError('fail to crawl js_ks, need retry...')
    _parse_and_fill(response.text, refer, now)
    return True


def main(d):
    while True:
        try:
            crawl_from_opencai(LOTTERY_TYPE.SD_11X5)
        except Exception as e:
            logging.exception(u'%s error: %s' % (OPENCAI_LOTTERY_TYPES.get(LOTTERY_TYPE.SD_11X5), e))
        if d:
            break
        time.sleep(60)


if __name__ == '__main__':
    date = sys.argv[1] if len(sys.argv) > 1 else None
    main(date)
